#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QStringList serialNamePort;

    this->setWindowTitle("serial_led");

    // 创建一个串口对象
    serialPort = new QSerialPort(this);

    // 搜索所有可用串口
    foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts()){
        serialNamePort<<info.portName();
    }
    ui->serialBox->addItems(serialNamePort);

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_openBtn_clicked()
{
     /* 串口设置 */
    serialPort->setPortName(ui->serialBox->currentText());
    serialPort->setBaudRate(ui->baudrateBox->currentText().toInt());
    serialPort->setDataBits(QSerialPort::Data8);
    serialPort->setStopBits(QSerialPort::OneStop);
    serialPort->setParity(QSerialPort::NoParity);
    // 打开串口提示框
    if(true == serialPort->open((QIODevice::ReadWrite))){
        QMessageBox::information(this,"提示","串口打开成功");
    }
    else{
        QMessageBox::information(this,"提示","串口打开失败");

    }


}

void Widget::on_closeBtn_clicked()
{
    serialPort->close();
}

void Widget::on_onBtn_clicked()
{
    serialPort->write("ON\n");
    qDebug("ON\n");
}

void Widget::on_offBtn_clicked()
{
    serialPort->write("OFF\n");
    qDebug("OFF\n");
}
